import re


class Solution:
    def simplifyPath(self, path: str) -> str:
        path = re.sub(r'/{2,}', '/', path)
        if path[-1] == '/':
            path = path[:-1]
        sp = path[1:].split('/')
        ans = []
        for p in sp:
            if p == '..':
                if ans:
                    ans.pop()
                continue
            elif p == '.':
                continue
            ans.append(p)
        return '/' + '/'.join(ans)


if __name__ == '__main__':
    s = Solution()
    r = s.simplifyPath("/home//foo/")
    print(r)
